package 链表;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 给定一个已排序的链表的头 head ， 删除原始链表中所有重复数字的节点，只留下不同的数字 。返回 已排序的链表 。
 * <p>
 * 1、
 * <p/>
 * User: zhaixiaolong Date:2024/1/15 9:07 ProjectName: algorithm Version:
 */
public class 删除重复元素 {

    /**
     * 初始化链表
     *
     * @return
     */
    public static Node initData() {
        Node node1 = new Node();
        node1.value = 1;
        Node node2 = new Node();
        node2.value = 2;
        Node node3 = new Node();
        node3.value = 3;
        Node node3_2 = new Node();
        node3_2.value = 3;
        Node node4 = new Node();
        node4.value = 4;
        Node node5 = new Node();
        node5.value = 5;

        node1.next = node2;
        node2.next = node3;
        node3.next = node3_2;
        node3_2.next = node4;
        node4.next = node5;
        return node1;
    }

    public static void main(String[] args) {
        Node head = initData();
        Node newNode = delRepeatNode(head);
    }

    public static Node delRepeatNode(Node head) {
        Node preNode = new Node();
        preNode.next = head;
        Node newHead = preNode;
        Node headTemp = newHead;
        while (head != null) {
            if (head.value != preNode.value && (head.next == null || head.value != head.next.value)) {
                newHead.next = head;
                newHead = head;
            }
            preNode = preNode.next;
            head = head.next;
        }
        newHead.next = null;
        return headTemp.next;
    }
}
